import os from '@packages/lib-os';

const [start, move, end] = os.phone ? ['touchstart', 'touchmove', 'touchend'] : ['mousedown', 'mousemove', 'mouseup'];

function getDataTouchNode($target) {
    if ($target.attr('data-touch')) {
        return $target;
    }

    return $($target.parents('[data-touch=touchEffect]')[0]);
}

// 触摸效果
$(window).on(start, (e) => {
    let $target = $(e.target);
    $target = getDataTouchNode($target);

    if ($target.attr('data-touch')) {
        $target.addClass('touchEffect');
        const handleTouchEnd = () => {
            $target.removeClass('touchEffect');
            // eslint-disable-next-line no-use-before-define
            $target.off(move, handleTouchMove);
            $target.off(end, handleTouchEnd);
        };
        const handleTouchMove = () => {
            $target.removeClass('touchEffect');
            $target.off(move, handleTouchMove);
        };
        $target.on(end, handleTouchEnd);
        $target.on(move, handleTouchMove);
    }
});
